package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.basic.web.service.LoginService;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.passport.param.AdminLoginParam;
import com.tarena.lbs.pojo.passport.param.UserLoginParam;
import com.tarena.lbs.pojo.passport.vo.LoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/passport/")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @GetMapping("test")
    public String test(){
        log.debug("success");
        return "haha";
    }
    //读数据操作 控制层入参(param query) 出参(**VO)
    //业务入参(param query) 出参(读 BO)
    //仓储层 入参(param query) 出参(读 BO)
    //持久层 入参(查询 query 写入 转化成DO) 出参(读 DO)
    //根据接口文件定义 http接口方法接收登录请求
    @PostMapping("admin/login")
    public Result<LoginVO> adminLogin(AdminLoginParam param) throws BusinessException {
        log.debug("C1-登录参数:{}",param);
        //调用业务层 判断登录结果,登录成功 给我返回查询到 业务结果 AdminBO
        AdminBO adminBO=loginService.adminDoLogin(param);
        //控制层将bo转化成vo
        LoginVO vo=new LoginVO();
        BeanUtils.copyProperties(adminBO, vo);
        return  new Result<>(vo);
    }

    //小程序登录
    @PostMapping("user/login")
    public Result<LoginVO> userLogin(UserLoginParam param) throws BusinessException {
        //调用业务层 判断登录结果,登录成功 给我返回查询到 业务结果 AdminBO
        UserBO userBO=loginService.userDoLogin(param);
        //控制层将bo转化成vo
        LoginVO vo=new LoginVO();
        BeanUtils.copyProperties(userBO, vo);
        return  new Result<>(vo);
    }


}
